package com.etcbase.dcs.controller.lookup;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.IntentService;
import android.content.Intent;
import android.os.ResultReceiver;
import android.util.Log;

import com.etcbase.dcs.DcsApp;
import com.etcbase.dcs.model.lookup.ContactType;
import com.etcbase.dcs.model.lookup.LookupServiceData;
import com.etcbase.dcs.model.lookup.Lookupable;
import com.etcbase.dcs.model.lookup.ServiceFileType;
import com.etcbase.dcs.model.lookup.Tenant;

public class LookupService extends IntentService {
	
	public static final String TAG = "LookupService";

	private List<Lookupable> serviceFileTypeList;
	private List<Lookupable> tenantList;
	private List<Lookupable> contactTypeList;
	private Map<String, Lookupable> lookupRepositoryMap;
	private Map<String, List<Lookupable>> lookupRepositoryListMap;

	public LookupService() {
		super("LookupService");
	}

	
	protected void fillServiceFileType() {
		ServiceFileType serviceFileTypeCollectionFile = new ServiceFileType();
		serviceFileTypeCollectionFile.setEnumValue("COLLECTION_FILE");
		serviceFileTypeCollectionFile.setId(100121490000002000L);
		serviceFileTypeCollectionFile.setName("Ä°dari Takip");
		serviceFileTypeList.add(serviceFileTypeCollectionFile);

		ServiceFileType serviceFileTypeLegalFollowupFile = new ServiceFileType();
		serviceFileTypeLegalFollowupFile.setEnumValue("LEGAL_FOLLOW_UP_FILE");
		serviceFileTypeLegalFollowupFile.setId(100121490000002001L);
		serviceFileTypeLegalFollowupFile.setName("Yasal Takip");
		serviceFileTypeList.add(serviceFileTypeLegalFollowupFile);

		ServiceFileType serviceFileTypeCaseFile = new ServiceFileType();
		serviceFileTypeCaseFile.setEnumValue("CASE_FILE");
		serviceFileTypeCaseFile.setId(100121490000002002L);
		serviceFileTypeCaseFile.setName("Dava");
		serviceFileTypeList.add(serviceFileTypeCaseFile);
	}

	protected void fillTenant() {
		Tenant tenantSystem = new Tenant();
		tenantSystem.setEnumValue("SYSTEM");
		tenantSystem.setId(10011004000000001L);
		tenantSystem.setName("System");
		tenantList.add(tenantSystem);

		Tenant tenantCollecturkHsbc = new Tenant();
		tenantCollecturkHsbc.setEnumValue("COLLECTURK_HSBC");
		tenantCollecturkHsbc.setId(10011004000001137L);
		tenantCollecturkHsbc.setName("Hsbc");
		tenantList.add(tenantCollecturkHsbc);

		Tenant tenantCollecturkKocFiat = new Tenant();
		tenantCollecturkKocFiat.setEnumValue("COLLECTURK_KOC_FIAT");
		tenantCollecturkKocFiat.setId(10011004000001156L);
		tenantCollecturkKocFiat.setName("KocFiat");
		tenantList.add(tenantCollecturkKocFiat);

	}

	protected void fillContactType() {
		ContactType contactTypePerson = new ContactType();
		contactTypePerson.setEnumValue("PERSON");
		contactTypePerson.setId(10011135000000057L);
		contactTypePerson.setName("KiÅŸi");
		contactTypeList.add(contactTypePerson);

		ContactType contactTypeCorporation = new ContactType();
		contactTypeCorporation.setEnumValue("CORPORATION");
		contactTypeCorporation.setId(10011135000000058L);
		contactTypeCorporation.setName("Å�irkey");
		contactTypeList.add(contactTypeCorporation);

	}

	protected void fillLookupRepositoryMap() {
		for (Lookupable lookupable : serviceFileTypeList) {

			lookupRepositoryMap.put(lookupable.getEnumValue(), lookupable);

		}
		for (Lookupable lookupable : contactTypeList) {

			lookupRepositoryMap.put(lookupable.getEnumValue(), lookupable);

		}

		for (Lookupable lookupable : tenantList) {

			lookupRepositoryMap.put(lookupable.getEnumValue(), lookupable);

		}

	}

	protected void fillLookupRepositoryListMap() {

		lookupRepositoryListMap.put("Tenant", tenantList);
		lookupRepositoryListMap.put("ServiceFileType", serviceFileTypeList);
		lookupRepositoryListMap.put("ContactType", serviceFileTypeList);

	}

	public List<Lookupable> getLookupableList(String clazzName)
	{
		
		return lookupRepositoryListMap.get(clazzName);
	}
	
	public Lookupable getLookupable(String enumValue)
	{
		
		return lookupRepositoryMap.get(enumValue);
	}
	
	// 
	private String performRestRequest() throws Exception {
		//TODO: replace the parameters with the ones corresponding to your REST call
		String responseString=null;
		String queryAddress = "http://10.0.2.2:8080/users/22";
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute( new HttpGet(queryAddress) );
		StatusLine statusLine = response.getStatusLine();
		int statusCode = statusLine.getStatusCode();
		if(statusCode == HttpStatus.SC_OK) {
		    ByteArrayOutputStream out = new ByteArrayOutputStream();
			response.getEntity().writeTo(out);
			out.close();
		    responseString = out.toString();
		    //..more logic
		} else {
		    //Closes the connection.
			response.getEntity().getContent().close();
		}
		
		return responseString;
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		
		Log.d(TAG, "***** onHandleIntent - START");
		
		DcsApp app = (DcsApp) getApplication();
		LookupServiceData lookupServiceData = app.getLookupServiceData();
		synchronized( lookupServiceData ) {
			serviceFileTypeList = lookupServiceData.serviceFileTypeList;
			tenantList = lookupServiceData.tenantList;
			contactTypeList = lookupServiceData.serviceFileTypeList;
			lookupRepositoryMap = lookupServiceData.lookupRepositoryMap;
			lookupRepositoryListMap = lookupServiceData.lookupRepositoryListMap;
			
			// TODO Auto-generated method stub
			fillServiceFileType();
			fillTenant();
			fillContactType();
			fillLookupRepositoryMap();
			fillLookupRepositoryListMap();
			
			// TODO: rest-call to retrieve data from the server is here: uncomment the following
//			String response=null;
//			try {
//				response = performRestRequest();
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		}
		Log.d(TAG, "***** onHandleIntent - END");
		
		ResultReceiver resultReceiver = intent.getParcelableExtra("resultReceiver");
		resultReceiver.send(0, null);
	}
		
	/*public static void main(String[] args) {
		LookupService lookupService = new LookupService();
		lookupService.fillContactType();
		lookupService.fillServiceFileType();
		lookupService.fillTenant();
		lookupService.fillLookupRepositoryListMap();
		List<Lookupable> listTenantTest = lookupService.getLookupableList("Tenant");
		System.out.println(listTenantTest.size());

	}*/

}
